// index.js
var app = getApp(),
  core = app.requirejs("core"),
  a = (app.requirejs("icons"), app.requirejs("foxui")),
  ij = (app.requirejs("icons"), app.requirejs("jquery")),
  n = 1,d=[],r=[];
Page({
  /**
   * 页面的初始数据
   */
  data: {
    route: "category",
    category: {},
    icons: app.requirejs("icons"),
    selector: 0,
    advimg: "",
    recommands: {},
    level: 0,
    back: 0,
    child: {},
    parent: {},
    goods:{},    
    // 下面是购物车用
    pickerOption:{},//规格
    diyform:{
      fields:"" ,
      f_data:""
    },
    tempname:"select-picker",
    active: "active",
    slider: "out",
    buyType:"cart",
    canBuy:"",
    thumb:"",
    specsTitle:"",
    options:{},
    optionid: 0,
    defaults: {
      id: 0,
      merchid: 0
    },
    toView: "",
  },

  /**
   * 分类函数--标签切换
   */
  tabCategory: function (t) {    
    let curTag = t.target.dataset.id;
    this.setData({
      selector: t.target.dataset.id,
      advimg: t.target.dataset.src,
      child: t.target.dataset.child,
      toView: 'inView'+curTag,
      back: 0
    }),
      ij.isEmptyObject(t.target.dataset.child) ? this.setData({
        level: 0
      }) : this.setData({
        level: 1
      })
  },

  /**
   * 分类函数--更新当前数据
   */
  cateChild: function (t) {
    this.setData({
      parent: t.currentTarget.dataset.parent,
      child: t.currentTarget.dataset.child,
      back: 1
    })
  },

  /**
   * 分类函数--更新上级数据
   */
  backParent: function (t) {
    this.setData({
      child: t.currentTarget.dataset.parent,
      back: 0
    })
  },

  /**
   * 分类函数-获取并更新分类数据
   */
  getCategory: function () {
    var t = this;
    core.get("shop/get_category", {}, function (e) {
      console.log(e);
      //这里需要重新整理购物车数据  当前商品多少样在购物车
      t.setData({
        category: e.category,
        show: true,
        set: e.set,
        advimg: e.set.advimg,
        recommands: e.recommands,
        child: e.recommands,
        level:e.set.level
      })
    })
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    console.log(this.data.slider);
    this.getCategory()
  },

  onHide:function(){
    console.log(this.data.slider);
    this.setData({
      active: "active",
      slider: "out",
    })
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
    return core.onShareAppMessage()
  },


  //减购物车
  reduce:function(event){
    var self = this;
    var goods = event.target.dataset.goods;
    self.setData({
      goods: goods
    })
    var id = goods.id;
    
    if (goods.cartnum == 0) return;


    core.get("goods/get_picker", {
      id: id
    }, function (t) {
      //n > 0 && (d = t.options, 
      (self.setData({
        pickerOption: t,
        active: "active",
        slider: "in",
      })),
        // 这个不知道是什么？
        t.diyform && self.setData({
          diyform: {
            fields: t.diyform.fields,
            f_data: t.diyform.lastdata
          }
        })
    })




  },
  //添加购物车
  plus: function (event){
    var goods = event.target.dataset.goods;
    // console.log(goods);
    var self = this;
    self.setData({
      goods: goods,
    })
    var id = goods.id;
    core.get("goods/get_picker", {
      id: id
    }, function (t) {
      if  (t.options.length>0){
        n > 0 && (d = t.options, self.setData({
          pickerOption: t,
          slider: "in"
        })),
          // 这个不知道是什么？
          t.diyform && self.setData({
            diyform: {
              fields: t.diyform.fields,
              f_data: t.diyform.lastdata
            }
          })
      }else{
          self.setData({
            "options.goodsid":goods.id,
            buyType: "cart",
            "options.id":0
          });
          self.getCart([]);
      }
    })
  },
  //规格选择
  specsTap: function (t) {
    var e = this,
      a = t.target.dataset.idx;
    r[a] = {
      id: t.target.dataset.id,
      title: t.target.dataset.title
    };
    var o = "",
      i = "";
    r.forEach(function (t) {
      o += t.title + ";",
        i += t.id + "_"
    }),
      i = i.substring(0, i.length - 1),
      d.forEach(function (a) {
        a.specs == i && (e.setData({
          options:a,
          buyType:"cart",
          optionid: a.id,
          "goods.total": a.stock,
          "goods.maxprice": a.marketprice,
          "goods.minprice": a.marketprice
        }), "" != t.target.dataset.thumb && e.setData({
          "goods.thumb": t.target.dataset.thumb
        }), a.stock <= 0 ? e.setData({
          canBuy: "库存不足"
        }) : e.setData({
          canBuy: ""
        }))
      }),
      e.setData({
        specsData: r,
        specsTitle: o
      })
  },
  //加入购物车
  getCart: function (t) {
    var i = this,
      s = i.data.options,
      r = i.data.diyform;
      console.log(r);
    if (0 == s) //n > 0 &&
      return void a.toast(i, "请选择规格");
    if (r && r.fields.length > 0) {
      if (!o.verify(i, r))
        return;
      core.post("order/create/diyform", {
        id: i.data.options.id,
        diyformdata: r.f_data
      }, function (t) {
        core.post("member/cart/add", {
          id: i.data.options.id,
          total: i.data.total,
          optionid: s,
          diyformdata: r.f_data
        }, function (t) {
          0 == t.error && i.setData({
            "goods.cartcount": t.cartcount,
            active: "",
            slider: "out"
          })
        })
      })
    } else
      core.post("member/cart/add", {
        id: s.goodsid,
        total: i.data.total ? i.data.total:1,
        optionid: s.id
      }, function (t) {
        if (0 == t.error){
          //刷新UI
          var category = i.data.category;
          for (var ii=0;ii<category.length;ii++){
            for (var i1 = 0; i1 < category[ii].child.length;i1++){
              if (category[ii].child[i1].id == s.goodsid) {
                category[ii].child[i1].cartnum += 1;
              }
            }
          }
          i.setData({
            category: category,
            "goods.cartcount": t.cartcount,
            active: "",
            slider: "out"
          })
        }

      })
  }
})